﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using MODEL;
namespace DAL
{
    public class IndentDal : IDAL.IIndentDal
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="count"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<MODEL.Indent> IndentShow(int pageindex, int pagesize, out int count, string name)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("identfy", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter[] p ={
                                      new SqlParameter("@pageindex",SqlDbType.Int),
                                      new SqlParameter("@pagesize",SqlDbType.Int),
                                      new SqlParameter("@count",SqlDbType.Int),
                                      new SqlParameter("@name",SqlDbType.VarChar),
                                 };
                p[0].Value = pageindex;
                p[1].Value = pagesize;
                p[2].Direction = ParameterDirection.Output;
                p[3].Value = name;
                cmd.Parameters.AddRange(p);

                count = Convert.ToInt32(p[2].Value);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                var list2 = JsonConvert.SerializeObject(dt);
                var list = JsonConvert.DeserializeObject<List<Indent>>(list2);
                return list;

                //if(dr.HasRows())
                //{
                //    while(dr.Read())
                //    {
                //        Indent ind = new Indent();
                //        ind.iid = (int)dr["iid"];    
                //       ind.name=dr["name"].ToString();     
                //       ind.addr=dr["addr"].ToString();      
                //       ind.tel=dr["tel"].ToString();       
                //       ind.phone=dr["phone"].ToString();     
                //       ind.postcords=dr["postcords"].ToString(); 
                //       ind.leverwords=dr["leverwords"].ToString();
                //       ind.orderdate=Convert.ToDateTime(dr["orderdate"]);
                //        ind.ordernum =(int)dr["ordernum"]; 
                //        ind.totalmoney=(float)dr["totalmoney"];
                //        ind.flag=(int)dr["flag"];      
                //         ind.sheng=dr["sheng"].ToString();    
                //        ind.shi=dr["shi"].ToString();       
                //         xian      
                //          pid      
                //        sid
                //    }
                //}
            }
        }


        /// <summary>
        /// 查询商品
        /// </summary>
        /// <returns></returns>
        public List<Shopping> shopping()
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                string sql = "select * from shopping";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader dr = cmd.ExecuteReader();
                List<Shopping> list = new List<Shopping>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Shopping s = new Shopping();
                        s.goodsname = dr["goodsname"].ToString();
                        s.sid = (int)dr["sid"];
                        s.image = dr["image"].ToString();
                        s.price = (float)dr["price"];
                        s.num = (int)dr["num"];
                        list.Add(s);
                    }
                }
                return list;
            }
        }
        /// <summary>
        /// 根据收货地址反填
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        public List<MODEL.Indent> show(string addr)
        {
            List<MODEL.Indent> listindent = RedisHelper.Get<List<MODEL.Indent>>("showindent");
            return listindent.Where(m => m.addr == addr).ToList();
            //if (listindent == null || listindent.Count == 0)
            //{
            //    using (SqlConnection con = new SqlConnection(constr))
            //    {
            //        string str = "select * from indent where addr='" + addr + "'";
            //        SqlDataAdapter da = new SqlDataAdapter(str, con);
            //        DataTable dt = new DataTable();
            //        da.Fill(dt);
            //        var list2 = JsonConvert.SerializeObject(dt);
            //        var list = JsonConvert.DeserializeObject<List<MODEL.Indent>>(list2);
            //        RedisHelper.Set("showaddr", list);
            //        return list;
            //    }
            //}
            //else
            //{
            //    return listindent;
            //}
        }
        /// <summary>
        /// 绑定收货地址下拉框
        /// </summary>
        /// <returns></returns>
        public List<MODEL.Indent> show()
        {
            List<MODEL.Indent> listindent = RedisHelper.Get<List<MODEL.Indent>>("showindent");
            return listindent;
            //if (listindent == null || listindent.Count == 0)
            //{
            //    using (SqlConnection con = new SqlConnection(constr))
            //    {
            //        string str = "select * from indent";
            //        SqlDataAdapter da = new SqlDataAdapter(str, con);
            //        DataTable dt = new DataTable();
            //        da.Fill(dt);
            //        var row = dt.NewRow();
            //        row[0] = 0;
            //        row[1] = "请选择";
            //        dt.Rows.InsertAt(row, 0);
            //        var list2 = JsonConvert.SerializeObject(dt);
            //        var list = JsonConvert.DeserializeObject<List<MODEL.Indent>>(list2);
            //        RedisHelper.Set("showdrop", list);
            //        return list;
            //    }
            //}
            //else
            //{
            //    return listindent;
            //}
        }
        /// <summary>
        /// 批量添加订单
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int addall(List<MODEL.Indent> list)
        {
            List<MODEL.Indent> list2 = null;
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();
                string str = null;
                foreach (var s in list)
                {
                    str = "insert into indent values('" + s.name + "','" + s.addr + "','" + s.tel + "','" + s.phone + "','" + s.postcords + "','" + s.leverwords + "','" + s.orderdate + "'," + s.ordernum + "," + s.totalmoney + ",0,'" + s.sheng + "','" + s.shi + "','" + s.xian + "',1," + s.sid + ")";
                    SqlCommand com = new SqlCommand(str, con);
                    int i = com.ExecuteNonQuery();
                    list2.Add(s);
                }
                RedisHelper.Set("showindent", list2);
                return 1;
            }
        }

        public int add(MODEL.Indent model)
        {
            throw new NotImplementedException();
        }

        public int del(int id)
        {
            throw new NotImplementedException();
        }

        public MODEL.BaseModel updid(int id)
        {
            throw new NotImplementedException();
        }

        public int upd(MODEL.Indent model)
        {
            throw new NotImplementedException();
        }
    }
}
